package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.Comment;
import com.cskaoyan.model.CommentExample;
import com.cskaoyan.model.User;
import com.cskaoyan.model.bo.WxCommentBO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.UserInfo;
import com.cskaoyan.model.vo.WxCommentCountVO;
import com.cskaoyan.model.vo.WxCommentDetailsVO;
import com.cskaoyan.service.WxCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: WxCommentServiceImpl
 * @Description: WxCommentServiceImpl
 * @Author:YPS
 * @Date: 2022/5/9 21:14
 * @Version: 1.0
 **/
@Service
@Transactional
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;
    @Override
    public DataVO<WxCommentDetailsVO> getCommentList(WxCommentBO wxCommentBO) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(wxCommentBO.getValueId());
        criteria.andDeletedEqualTo(false);
        commentExample.setOrderByClause("add_time" + " " + "desc");
        if (wxCommentBO.getShowType() == 1){
            criteria.andHasPictureEqualTo(true);
        }
        List<WxCommentDetailsVO> wxCommentDetailsVOList = new ArrayList<>();
        PageHelper.startPage(wxCommentBO.getPage(), wxCommentBO.getLimit());
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        Long total = pageInfo.getTotal();
        for (Comment comment : commentList) {
            WxCommentDetailsVO wxCommentDetailsVO = new WxCommentDetailsVO();
            wxCommentDetailsVO.setAddTime(comment.getAddTime());
            wxCommentDetailsVO.setAdminContent(comment.getAdminContent());
            wxCommentDetailsVO.setContent(comment.getContent());
            wxCommentDetailsVO.setPicList(comment.getPicUrls());
            UserInfo userInfo = new UserInfo();
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            userInfo.setAvatarUrl(user.getAvatar());
            userInfo.setNickName(user.getNickname());
            wxCommentDetailsVO.setUserInfo(userInfo);
            wxCommentDetailsVOList.add(wxCommentDetailsVO);
        }
        DataVO<WxCommentDetailsVO> commentListVODataVO = new DataVO<>();
        commentListVODataVO.setLimit(wxCommentBO.getLimit());
        commentListVODataVO.setPages(pageInfo.getPages());
        commentListVODataVO.setPage(wxCommentBO.getPage());
        commentListVODataVO.setTotal(Math.toIntExact(total));
        commentListVODataVO.setList(wxCommentDetailsVOList);
        return commentListVODataVO;
    }

    @Override
    public WxCommentCountVO getCommentCount(Integer valueId, Integer type) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andDeletedEqualTo(false);
        WxCommentCountVO commentCountVO = new WxCommentCountVO();
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        Integer count = 0;
        Integer hasPic = 0;
        for (Comment comment : commentList) {
            if (comment.getHasPicture() == true){
                hasPic++;
            }
        }
        count = commentList.size();
        commentCountVO.setAllCount(count);
        commentCountVO.setHasPicCount(hasPic);
        return commentCountVO;
    }

    @Override
    public Comment postComment(Comment comment) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = userMapper.selectUserId(username);
        comment.setUserId(userId);
        Date date = new Date();
        comment.setAddTime(date);
        comment.setUpdateTime(date);
        comment.setDeleted(false);
        commentMapper.insertSelective(comment);
        return comment;
    }
}
